package com.ofsoft.weixin.mp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ofsoft.weixin.core.WeixinProperties;
import com.ofsoft.weixin.mp.api.AccessTokenConfig;
import com.ofsoft.weixin.mp.api.cache.IAccessTokenCache;
import com.ofsoft.weixin.mp.message.MessageController;

/**
 * 公共配置文件(单例)
 * 
 * @author OF
 * @date 2017年10月10日
 */
public class AppConfig {
	private static final Logger log = LoggerFactory.getLogger(AppConfig.class);
	private static AppConfig appConfig = new AppConfig();

	private AppConfig() {

	}

	public static AppConfig getInstance() {
		return appConfig;
	}

	/** 应用唯一标识 */
	public String appId;
	/** 密钥 */
	public String appSecret;
	/** 令牌 */
	public String appToken;
	/** 访问token */
	public String appAccessToken;
	/** 超时时间 */
	public String appTimeout = "3000";
	/** 编码 */
	public String appCharset = "utf-8";
	/** 默认配置文件路径 */
	public String configFile = "weixin.properties";

	public static String getConfigFile() {
		return appConfig.configFile;
	}

	public void setConfigFile(String configFile) {
		this.configFile = configFile;
	}

	public static String getAppId() {
		return appConfig.appId;
	}

	public static String getAppSecret() {
		return appConfig.appSecret;
	}

	public static String getAppToken() {
		return appConfig.appToken;
	}

	public static String getAppAccessToken() {
		return appConfig.appAccessToken;
	}

	public static String getAppTimeout() {
		return appConfig.appTimeout;
	}

	public static String getAppCharset() {
		return appConfig.appCharset;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public void setAppToken(String appToken) {
		this.appToken = appToken;
	}

	public void setAppAccessToken(String appAccessToken) {
		this.appAccessToken = appAccessToken;
	}

	public void setAppTimeout(String appTimeout) {
		this.appTimeout = appTimeout;
	}

	public void setAppCharset(String appCharset) {
		this.appCharset = appCharset;
	}

	public AppConfig setMessageController(Class<?> messageController) {
		HandlerFactory.setMessageController(messageController);
		return this;
	}

	public AppConfig setMessageController(MessageController messageController) {
		HandlerFactory.setMessageController(messageController);
		return this;
	}

	public AppConfig setAccessTokenCache(IAccessTokenCache accessTokenCache) {
		AccessTokenConfig.setAccessTokenCache(accessTokenCache);
		return this;
	}

	public AppConfig init(MessageController messageController) {
		setMessageController(messageController);
		init();
		return this;
	}

	public AppConfig init(Class<?> messageController) {
		setMessageController(messageController);
		init();
		return this;
	}

	/**
	 * 配置文件初始化
	 */
	public AppConfig init() {
		// 加载配置文件
		log.debug("init weixin config file start");
		// 加载配置文件
		WeixinProperties.init();
		// 初始化常量
		this.setAppCharset(WeixinProperties.getPropertyValue("wx_app_charset"));
		this.setAppSecret(WeixinProperties.getPropertyValue("wx_app_secret"));
		this.setAppId(WeixinProperties.getPropertyValue("wx_app_id"));
		this.setAppToken(WeixinProperties.getPropertyValue("wx_app_token"));
		this.setAppTimeout(WeixinProperties.getPropertyValue("wx_app_timeout"));
		log.debug("init weixin config file end");
		return this;
	}

	/**
	 * 设置配置文件
	 * 
	 * @param appId
	 * @param appSecret
	 * @param appToken
	 * @param appTimeout
	 * @param appCharset
	 * @return
	 */
	public AppConfig setConfig(String appId, String appSecret, String appToken,
			String appTimeout, String appCharset) {
		appConfig.appId = appId;
		appConfig.appSecret = appSecret;
		appConfig.appToken = appToken;
		appConfig.appTimeout = appTimeout;
		appConfig.appCharset = appCharset;
		return this;
	}

	public AppConfig setConfig(String appId, String appSecret, String appToken) {
		appConfig.appId = appId;
		appConfig.appSecret = appSecret;
		appConfig.appToken = appToken;
		return this;
	}

}
